package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.home.dto.ReportDrawingCategoryAuditDTO;
import com.eastfair.venueservice.dao.ReportVenueDrawingCategoryAuditMapper;
import com.eastfair.venueservice.dto.ReportVenueDrawingAuditDTO;
import com.eastfair.venueservice.dto.ReportVenueDrawingCategoryAuditDTO;
import com.eastfair.venueservice.entity.ReportVenueDrawingAudit;
import com.eastfair.venueservice.entity.ReportVenueDrawingCategoryAudit;
import com.eastfair.venueservice.enumeration.ReportVenueDrawingCategoryAuditAuditStatusEnum;
import com.eastfair.venueservice.service.ReportVenueDrawingAuditService;
import com.eastfair.venueservice.service.ReportVenueDrawingCategoryAuditHistoryService;
import com.eastfair.venueservice.service.ReportVenueDrawingCategoryAuditService;
import com.eastfair.venueservice.vo.ReportVenueDrawingCategoryAuditVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 一键报馆图纸分类审核记录
 * </p>
 *
 * @author dq
 * @date 2022-09-07
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportVenueDrawingCategoryAuditServiceImpl extends SuperServiceImpl<ReportVenueDrawingCategoryAuditMapper, ReportVenueDrawingCategoryAudit> implements ReportVenueDrawingCategoryAuditService {

    @Resource
    private ReportVenueDrawingAuditService reportVenueDrawingAuditService;

    @Resource
    private ReportVenueDrawingCategoryAuditHistoryService reportVenueDrawingCategoryAuditHistoryService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportVenueDrawingCategoryAudit> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportVenueDrawingCategoryAudit model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());

        model.setAuditStatus(ReportVenueDrawingCategoryAuditAuditStatusEnum.SUBMIT);
        model.setReportTime(LocalDateTime.now());
        return R.successDef();
    }

    @Override
    public List<ReportVenueDrawingCategoryAuditVO> listVOByReportVenueSpaceNumber(String reportVenueSpaceNumber) {
        log.info("listVOByReportVenueSpaceNumber - 报馆号查询图纸分类审核VO列表, reportVenueSpaceNumber={}", reportVenueSpaceNumber);
        List<ReportVenueDrawingCategoryAudit> list = listByReportVenueSpaceNumber(reportVenueSpaceNumber);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list
                .stream()
                .map(reportVenueDrawingCategoryAudit -> BeanUtil.toBean(reportVenueDrawingCategoryAudit, ReportVenueDrawingCategoryAuditVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<ReportVenueDrawingCategoryAudit> listByReportVenueSpaceNumber(String reportVenueSpaceNumber) {
        log.info("listByReportVenueSpaceNumber - 报馆号查询图纸分类审核列表, reportVenueSpaceNumber={}", reportVenueSpaceNumber);
        ReportVenueDrawingCategoryAuditDTO param = new ReportVenueDrawingCategoryAuditDTO();
        param.setReportVenueSpaceNumber(reportVenueSpaceNumber);
        return listReportVenueDrawingCategoryAudit(param);
    }

    @Override
    public List<ReportVenueDrawingCategoryAudit> listByIds(List<Long> ids) {
        ReportVenueDrawingCategoryAuditDTO param = new ReportVenueDrawingCategoryAuditDTO();
        param.setIds(ids);
        return listReportVenueDrawingCategoryAudit(param);
    }

    @Override
    public List<ReportVenueDrawingCategoryAudit> listReportVenueDrawingCategoryAudit(ReportVenueDrawingCategoryAuditDTO param) {
        log.info("listReportVenueDrawingCategoryAudit - 查询图纸分类审核列表, param={}", param);
        QueryWrapper<ReportVenueDrawingCategoryAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ReportVenueDrawingCategoryAudit::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(StrUtil.isNotBlank(param.getReportVenueSpaceNumber()), ReportVenueDrawingCategoryAudit::getReportVenueSpaceNumber, param.getReportVenueSpaceNumber())
                .in(param.getIds() != null && !param.getIds().isEmpty(), ReportVenueDrawingCategoryAudit::getId, param.getIds())
                .orderByAsc(ReportVenueDrawingCategoryAudit::getDrawingCategorySort)
        ;
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveReportVenueDrawingCategoryAudit(ReportVenueDrawingCategoryAuditDTO auditDTO) {
        log.info("saveReportVenueDrawingCategoryAudit - 保存图纸分类审核, auditDTO={}", auditDTO);
        ReportVenueDrawingCategoryAudit reportVenueDrawingCategoryAudit = BeanUtil.toBean(auditDTO, ReportVenueDrawingCategoryAudit.class);
        save(reportVenueDrawingCategoryAudit);
        // 保存图纸审核列表
        List<ReportVenueDrawingAuditDTO> reportVenueDrawingAuditList = auditDTO.getReportVenueDrawingAuditList();
        reportVenueDrawingAuditList.forEach(reportVenueDrawingAuditDTO -> {
            reportVenueDrawingAuditDTO.setReportVenueSpaceAuditId(auditDTO.getReportVenueSpaceAuditId());
            reportVenueDrawingAuditDTO.setReportVenueDrawingCategoryAuditId(reportVenueDrawingCategoryAudit.getId());
        });
        return reportVenueDrawingAuditService.saveReportVenueDrawingAuditBatch(reportVenueDrawingAuditList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<ReportVenueDrawingCategoryAuditDTO> list, Long reportVenueSpaceAuditId) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        list.forEach(auditDTO -> {
            auditDTO.setReportVenueSpaceAuditId(reportVenueSpaceAuditId);
            saveReportVenueDrawingCategoryAudit(auditDTO);
        });
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitBatch(List<ReportVenueDrawingCategoryAuditDTO> list, Long reportVenueSpaceAuditId) {
        log.info("submitBatch - 批量提交图纸分类审核列表, list={}, reportVenueSpaceAuditId={}", list, reportVenueSpaceAuditId);
        if (list == null || list.isEmpty()) {
            return false;
        }
        ReportVenueDrawingCategoryAuditDTO reportVenueDrawingCategoryAuditDTO = list.get(0);
        String reportVenueSpaceNumber = reportVenueDrawingCategoryAuditDTO.getReportVenueSpaceNumber();
        removeByReportVenueSpaceNumber(reportVenueSpaceNumber);
        return saveBatch(list, reportVenueSpaceAuditId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBatchAuditStatus(List<ReportVenueDrawingCategoryAuditDTO> list) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        Map<Long, ReportVenueDrawingCategoryAuditDTO> auditMap = new HashMap<>(16);
        list.forEach(auditDTO -> auditMap.put(auditDTO.getId(), auditDTO));
        // 查询要更新的图纸类型列表
        List<Long> ids = new ArrayList<>(auditMap.keySet());
        List<ReportVenueDrawingCategoryAudit> reportVenueDrawingCategoryAuditList = listByIds(ids);
        // 批量更新图纸分类的审核状态
        reportVenueDrawingCategoryAuditList
                .forEach(reportVenueDrawingCategoryAudit -> {
                    ReportVenueDrawingCategoryAuditDTO auditDTO = auditMap.get(reportVenueDrawingCategoryAudit.getId());
                    reportVenueDrawingCategoryAudit.setAuditStatus(auditDTO.getAuditStatus());
                    reportVenueDrawingCategoryAudit.setAuditOpinion(auditDTO.getAuditOpinion());
                    reportVenueDrawingCategoryAudit.setAuditTime(LocalDateTime.now());
                    reportVenueDrawingCategoryAudit.setAuditUserId(ContextUtil.getUserId());
                    reportVenueDrawingCategoryAudit.setAuditUserName(ContextUtil.getRealName());
                });
        updateBatchById(reportVenueDrawingCategoryAuditList);
        // 查询图纸审核列表
        List<ReportVenueDrawingAudit> reportVenueDrawingAuditList = reportVenueDrawingAuditService.listByReportVenueDrawingCategoryAuditIds(ids);
        // 批量保存图纸分类审核历史
        return reportVenueDrawingCategoryAuditHistoryService.saveBatchByReportVenueDrawingCategoryAudit(reportVenueDrawingCategoryAuditList, reportVenueDrawingAuditList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByReportVenueSpaceNumber(String reportVenueSpaceNumber) {
        log.info("removeByReportVenueSpaceNumber - 报馆号删除图纸分类审核列表, reportVenueSpaceNumber={}", reportVenueSpaceNumber);
        List<ReportVenueDrawingCategoryAudit> list = listByReportVenueSpaceNumber(reportVenueSpaceNumber);
        if (list == null || list.isEmpty()) {
            return false;
        }
        removeByIdsOfLogic(list);
        return reportVenueDrawingAuditService.removeByReportVenueSpaceNumber(reportVenueSpaceNumber);
    }
}
